package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.dto.LLevelSonDTO;
import com.learn.core.service.ILLevelPaperService;
import com.learn.core.service.ILLevelQuestionsService;
import com.learn.core.service.ILLevelSonService;
import com.learn.core.service.ILQuestionsTypeService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LLevelSonController
 * @date 2021.07.09 10:01
 * @describe 子闯关
 */
@Api("子闯关")
@RestController
@RequestMapping("/manager/sLevel")
public class LLevelSonController extends WebController<LLevelSon> {
    @Autowired
    private ILLevelSonService ilLevelSonService;
    @Autowired
    private ILLevelQuestionsService ilLevelQuestionsService;
    @Autowired
    private ILQuestionsTypeService ilQuestionsTypeService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken()
    @PostMapping("/list")
    public PageResult<LLevelSon> list(@RequestBody LLevelSonDTO lLevelSonDTO){
        try {
            if (lLevelSonDTO==null||lLevelSonDTO.getPage()==null||lLevelSonDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lLevelSonDTO.getPage(),lLevelSonDTO.getRows());
            QueryWrapper<LLevelSon> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lLevelSonDTO.getName()))
                queryWrapper.like("level_son_name",lLevelSonDTO.getName());
            if(StringUtils.isNotEmpty(lLevelSonDTO.getStartTime()))
                queryWrapper.gt("create_time",lLevelSonDTO.getStartTime());
            if(StringUtils.isNotEmpty(lLevelSonDTO.getEndTime()))
                queryWrapper.le("create_time",lLevelSonDTO.getEndTime());
            Page<LLevelSon> lLevelSons = (Page<LLevelSon>)ilLevelSonService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lLevelSons)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lLevelSons.getTotal(), lLevelSons, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新子闯关", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LLevelSon lLevelSon, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lLevelSon!=null){
            lLevelSon.setUpdateTime(new Date());
            lLevelSon.setUpdateUser(userId);
            if(ilLevelSonService.updateById(lLevelSon)){
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加子闯关", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LLevelSon lLevelSon, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lLevelSon!=null) {
                if (StringUtils.isEmpty(lLevelSon.getLevelSonName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_LEVEL_NAME);
                }
                lLevelSon.setCreateTime(new Date());
                lLevelSon.setCreateUser(userId);
                if (ilLevelSonService.save(lLevelSon)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除子闯关", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LLevelSon lLevelSon){
        try {
            if(lLevelSon!=null){
                if(lLevelSon.getLevelSonId()!=null){
                    QueryWrapper<LLevelSon> queryWrapper = new QueryWrapper<LLevelSon>();
                    queryWrapper.in(true, "level_son_id", lLevelSon.getLevelSonId());
                    if(ilLevelSonService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增关卡抽题策略
    @ApiOperation(value = "新增关卡抽题策略")
    @PostMapping("/eqadd")
    @UserLoginToken
    @Log(title = "新增关卡抽题策略", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> eqadd(@RequestBody LLevelSon lLevelSon, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lLevelSon!=null) {
                if (StringUtils.isEmpty(lLevelSon.getLevelSonId())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.LOAD_ERROR);
                }
                // 修改抽题信息
                lLevelSon.setUpdateTime(new Date());
                lLevelSon.setUpdateUser(userId);
                if(ilLevelSonService.updateById(lLevelSon)) {
                    List<LLevelQuestions> levelQuestions = lLevelSon.getLevelQuestions();
                    for (LLevelQuestions levelQuestion : levelQuestions) {
                        if (StringUtils.isNotEmpty(levelQuestion.getLevelQuestionsId()))
                            ilLevelQuestionsService.updateById(levelQuestion);
                        else
                            ilLevelQuestionsService.save(levelQuestion);
                    }
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //查询关卡抽题策略
    @ApiOperation(value = "查询关卡抽题策略")
    @GetMapping("/eqinfo")
    @UserLoginToken
    public CommonResult<Object> eqinfo(@RequestParam("id")Integer id){
        try {
            QueryWrapper<LLevelQuestions> questionQueryWrapper = new QueryWrapper<LLevelQuestions>();
            questionQueryWrapper.eq("level_son_id",id);
            List<LLevelQuestions> list = ilLevelQuestionsService.list(questionQueryWrapper);
            for (LLevelQuestions levelQuestion: list) {
                LQuestionsType lQuestionsType = ilQuestionsTypeService.getById(levelQuestion.getQuestionsTypeId());
                if(null != lQuestionsType)
                    lQuestionsType.setQuestionsTypeName(lQuestionsType.getQuestionsTypeName());
            }
            return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS,list);
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //删除关卡抽题策略
    @ApiOperation(value = "删除关卡抽题策略")
    @PostMapping("/eqdel")
    @UserLoginToken
    @Log(title = "删除关卡抽题策略", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> eqdel(@RequestBody LLevelQuestions lLevelQuestions){
        try {
            if(lLevelQuestions!=null){
                if(lLevelQuestions.getLevelQuestionsId()!=null){
                    QueryWrapper<LLevelQuestions> queryWrapper = new QueryWrapper<LLevelQuestions>();
                    queryWrapper.eq("level_questions_id", lLevelQuestions.getLevelQuestionsId());
                    if(ilLevelQuestionsService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
